<?php

class CategoryController extends Controller {

    public function init() {
		$this->layout = '//layouts/category';

		parent::init();
    }

    public function actionIndex() {
		$category = $this->__check_get_params_category();

		$level_category = Items::get_level_nesting($category->id);

		if($level_category == 1){
			$this->__one_level_category($category);
		}elseif($level_category == 2){
			$this->__two_level_category($category);
		}elseif($level_category == 3){
			$this->__three_level_category($category);
		}else{
			$this->__one_level_category($category);
		}

    }
    
    private function __one_level_category($category){
	 $children_categories = Items::model()->findAll('parent_id =:parent_id and type_position =:type_position ', array(':parent_id' => $category->id, ':type_position' => Items::STATUS_TYPE_POSITION_FOLDER));

	 $this->sidebar_left_category_content = $this->renderPartial('sidebar_left_children_category', array('category' => $category,'children_categories' => $children_categories, 'active_category' => $category), true);

	 $this->render('level_one', array('category' => $category, 'children_categories' => $children_categories));
    }

    private function __two_level_category($category){
	 $sibling_categories = Items::model()->findAll('parent_id =:parent_id and type_position =:type_position ', array(':parent_id' => $category->parent_id, ':type_position' => Items::STATUS_TYPE_POSITION_FOLDER));

	 $children_categories = Items::model()->findAll('parent_id =:parent_id and type_position =:type_position ', array(':parent_id' => $category->id, ':type_position' => Items::STATUS_TYPE_POSITION_FOLDER));

	 $parent_category = Items::model()->findByPk($category->parent_id);

	 $this->sidebar_left_category_content = $this->renderPartial('sidebar_left_children_category', array('category' => $parent_category,'children_categories' => $sibling_categories, 'active_category' => $category), true);

	 //вигрібаю товари
	 $items = array();
	 $criteria = new CDbCriteria();
	 $criteria->condition = ' type_position =:type_position and status =:status';
	 $criteria->params = array(':type_position' => Items::STATUS_TYPE_POSITION_ITEM, ':status' => Items::STATUS_ON_SITE);
	 $criteria->addInCondition('parent_id', CHtml::listData($children_categories, 'id','id'));
	 $criteria->order = 'id DESC';

	 $count = Items::model()->count($criteria);

	 $pages=new CPagination($count);
	 $pages->pageSize=Items::COUNT_VIEW_ITTEMS;
	 $pages->applyLimit($criteria);

	 $items = Items::model()->findAll($criteria);

	 $this->render('level_two', array('category' => $category, 'children_categories' => $children_categories,'pages' => $pages, 'items' => $items));
    }
    private function __three_level_category($category){
	//батьківська категорія
	 $parent_category = Items::model()->findByPk($category->parent_id);

	 //головна категорія
	 $parent_parent_category = Items::model()->findByPk($parent_category->parent_id);

	 //батьківська та сосідні категорії
	 $children_categories = Items::model()->findAll('parent_id =:parent_id and type_position =:type_position ', array(':parent_id' => $parent_category->parent_id, ':type_position' => Items::STATUS_TYPE_POSITION_FOLDER));

	 //сосідні категорії
	 $sibling_categories = Items::model()->findAll('parent_id =:parent_id and type_position =:type_position ', array(':parent_id' => $category->parent_id, ':type_position' => Items::STATUS_TYPE_POSITION_FOLDER));

	 //бокова панель
	 $this->sidebar_left_category_content = $this->renderPartial('sidebar_left_children_category', array('category' => $parent_parent_category,'children_categories' => $children_categories, 'active_category' => $parent_category), true);

	 //вигрібаю товари
	 $items = array();
	 $criteria = new CDbCriteria();
	 $criteria->condition = ' parent_id = :parent_id and type_position =:type_position and status =:status';
	 $criteria->params = array(':parent_id' => $category->id, ':type_position' => Items::STATUS_TYPE_POSITION_ITEM, ':status' => Items::STATUS_ON_SITE);
	 $criteria->order = 'id DESC';

	 $count = Items::model()->count($criteria);

	 $pages=new CPagination($count);
	 $pages->pageSize=  Items::COUNT_VIEW_ITTEMS;
	 $pages->applyLimit($criteria);

	 $items = Items::model()->findAll($criteria);

	 //рендер
	 $this->render('level_two', array('category' => $category, 'children_categories' => $sibling_categories,'pages' => $pages, 'items' => $items));
    }

    private function __check_get_params_category() {
		if (Yii::app()->request->getQuery('category_id') && Yii::app()->request->getQuery('category_title')) {
			$category = Items::model()->find(
				'type_position = :type_position and status =:status and id =:id',
				 array(
				 ':id' =>Yii::app()->request->getQuery('category_id'),
				 ':type_position' => Items::STATUS_TYPE_POSITION_FOLDER,
				 ':status' => Items::STATUS_ON_SITE )
			);

			if (empty($category)){
				$this->redirect(Yii::app()->controller->createUrl('error/index'));
			}

			if (!empty($category) && trim($category->title) == trim(Yii::app()->request->getQuery('category_title'))){
				return $category;
			}elseif (!empty($category) && trim($category->title) != trim(Yii::app()->request->getQuery('category_title'))){
				$this->redirect(Yii::app()->controller->createUrl('/category/index', array('category_id' => trim($category->id), 'category_title' => trim($category->title))), true, 301);
			}else{
				$this->redirect(Yii::app()->controller->createUrl('error/index'));
			}
		} else{
			$this->redirect(Yii::app()->controller->createUrl('error/index'));
		}
    }

}
